package org.example.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.crypto.digest.DigestUtil;
import org.example.dao.ShoppingUserMapper;
import org.example.dto.RegisterDTO;
import org.example.po.ShoppingUser;
import org.example.rvo.ResultEnum;
import org.example.rvo.ResultVO;
import org.example.service.ShoppingUserService;
import org.example.util.JWTUtil;
import io.jsonwebtoken.Claims;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.util.Map;

@Service
public class ShoppingUserServiceImpl implements ShoppingUserService {

    @Resource
    private ShoppingUserMapper shoppingUserMapper;
    
    @Override
    public ResultVO login(String name, String pass) throws Exception {
        ShoppingUser shoppingUser = shoppingUserMapper.queryUserByNameAndPass(name, null);
        String salt = "";
        if(shoppingUser != null){
                salt = shoppingUser.getUserSalt();
        }
        String password = DigestUtil.md5Hex(DigestUtil.md5Hex(pass)+salt);
        ShoppingUser loginUser = shoppingUserMapper.queryUserByNameAndPass(name, password);
        String token = JWTUtil.createJWT(loginUser.getId(), loginUser, "userLogin", null);
        if(loginUser != null){
            return ResultVO.success(ResultEnum.LOGIN_USER_SUCCESS.getResCode(),
                    ResultEnum.LOGIN_USER_SUCCESS.getResMsg(),token);
        }
        return ResultVO.failure(ResultEnum.LOGIN_USER_FAILURE.getResCode(),
                ResultEnum.LOGIN_USER_FAILURE.getResMsg());
    }

    @Override
    public ResultVO register(RegisterDTO registerDTO) throws Exception {
        ShoppingUser shoppingUser1 = shoppingUserMapper.queryUserByNameAndPass(registerDTO.getUserName(),null);
        if(shoppingUser1 == null){
            String salt = RandomUtil.randomNumbers(8);
            MultipartFile userHeadImg = registerDTO.getUserHeadImg();
            String originalFilename = userHeadImg.getOriginalFilename();
            String suffix = originalFilename.substring(originalFilename.lastIndexOf("."),originalFilename.length());
            String file = System.currentTimeMillis()+RandomUtil.randomNumbers(6)+suffix;
            userHeadImg.transferTo(new File("/Users/jianma/Documents/upload",file));
            ShoppingUser shoppingUser = new ShoppingUser();
            shoppingUser.setId(IdUtil.simpleUUID());
            shoppingUser.setUserName(registerDTO.getUserName());
            shoppingUser.setUserPass(DigestUtil.md5Hex(DigestUtil.md5Hex(registerDTO.getUserPass())+salt));
            shoppingUser.setUserSalt(salt);
            shoppingUser.setUserGender(registerDTO.getUserGender());
            shoppingUser.setUserPhone(registerDTO.getUserPhone());
            shoppingUser.setUserHeadImg(file);
            int i = shoppingUserMapper.insertShoppingUser(shoppingUser);
            if(i > 0){
                return ResultVO.success(ResultEnum.REGISTER_USER_SUCCESS.getResCode(),
                        ResultEnum.REGISTER_USER_SUCCESS.getResMsg());
            }
        }
        return ResultVO.failure(ResultEnum.REGISTER_USER_FAILURE.getResCode(),
                ResultEnum.REGISTER_USER_FAILURE.getResMsg());
    }

    @Override
    public ResultVO queryUser(ShoppingUser shoppingUser) throws Exception {
        return ResultVO.success("100011","查询成功",shoppingUser);

    }

    @Override
    public ResultVO updateUser(ShoppingUser shoppingUser) throws Exception {
        ShoppingUser shoppingUser1 = shoppingUserMapper.queryUserByNameAndPass(shoppingUser.getUserName(),null);
        String salt = null;
        if(shoppingUser1 != null){
                salt = shoppingUser1.getUserSalt();
                shoppingUser.setUserPass(DigestUtil.md5Hex(DigestUtil.md5Hex(shoppingUser.getUserPass())+salt));
        }
        System.out.println(shoppingUser.getUserPass());
        int i = shoppingUserMapper.updateUserPassInt(shoppingUser.getId(), shoppingUser.getUserPass());
        if(i > 0) {
            return ResultVO.success("200","修改成功");
        }
        return ResultVO.failure("500","修改失败");
    }
}
